#!/bin/bash

#Panel for BigLinux
#
#Authors:
#  Bruno Goncalves Araujo <www.biglinux.com.br>
#
#License: GPLv2 or later
#################################################


. /usr/share/bigcontrolcenter/default/theme-categories.sh.htm

#Translation
export TEXTDOMAINDIR="/usr/share/locale-langpack"
export TEXTDOMAIN=bigcontrolcenter-system-biguser

echo '
<script language="JavaScript">
window.resizeTo(640,480);
function NoResize(){
  window.resizeTo(640,480);
}
</script>
'

title=$"Trocar senha"
apply=$"Aplicar"
close=$"Sair"
#title


echo "<title>$title</title><body onResize=\"NoResize()\"; onLoad='document.signupform.username.disabled = true';>"

#header
open_header
echo "$title" '</font><br>' $"Escolha uma nova senha."
close_header

if [ "$p_username" = "" ]
then
  echo "<body onLoad=\"alert('" $"Selecione um usuario" "'), history.go(-2)\">"
fi

#O uso do echo pode ser separado em diversos pedacoes dentro do mesmo comando
#O uso de aspas simples faz o sistema exibir exatamente o que esta escrito
#Quando se utiliza aspas duplas o sistema executa algumas partes do codigo antes de passar a resposta, entre essas execucoes inclui a interpretacao de variaveis
#Para incluir textos a serem exibidos ao usuario utilize aspas duplas com $ antes, para que funcione o sistema de tradução, exemplo: $"texto"
#No exemplo abaixo o memso comando echo foi aberto e fechado diversas vezes de acordo com a necessidade, aspas simples, duplas e duplas com traducao.

echo '<link rel="stylesheet" type="text/css" media="screen" href="milk.css" />
<link rel="stylesheet" type="text/css" media="screen" href="jquery.validate.password.css" />

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
'

echo '<center><table width=95%><tr><td>'
echo $"Preencha os campos abaixo:"
echo "<p>"


cat << EOF

<script id="demo" type="text/javascript">
/*
 * jQuery validate.password plug-in 1.0
 *
 * http://bassistance.de/jquery-plugins/jquery-plugin-validate.password/
 *
 * Copyright (c) 2009 Jörn Zaefferer
 *
 * \$Id$
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function($) {
	
	var LOWER = /[a-z]/,
		UPPER = /[A-Z]/,
		DIGIT = /[0-9]/,
		DIGITS = /[0-9].*[0-9]/,
		SPECIAL = /[^a-zA-Z0-9]/,
		SAME = /^(.)\1+$/;
		
	function rating(rate, message) {
		return {
			rate: rate,
			messageKey: message
		};
	}
	
	function uncapitalize(str) {
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}
	
	$.validator.passwordRating = function(password, username) {
		if (!password || password.length < 5)
			return rating(1, "too-short");
		if (username && password.toLowerCase().match(username.toLowerCase()))
			return rating(1, "similar-to-username");
		if (SAME.test(password))
			return rating(1, "very-weak");
		
		var lower = LOWER.test(password),
			upper = UPPER.test(uncapitalize(password)),
			digit = DIGIT.test(password),
			digits = DIGITS.test(password),
			special = SPECIAL.test(password);
		
		if (lower && upper && digit || upper && digits || special)
			return rating(4, "strong");
		if (lower && upper || lower && digit || upper && digit)
			return rating(3, "good");
		return rating(2, "weak");
	}
	
	$.validator.passwordRating.messages = {
EOF
echo '		"similar-to-username": "' $"Similar ao nome de usuario" '",
		"too-short": "' $"Muito pequena" '",
		"very-weak": "' $"Muito simples" '",
		"weak": "' $"Simples" '",
		"good": "' $"Boa" '",
		"strong": "' $"Muito boa" '"
'
cat << EOF
	}
	
	$.validator.addMethod("password", function(value, element, usernameField) {
		// use untrimmed value
		var password = element.value,
		// get username for comparison, if specified
			username = \$(typeof usernameField != "boolean" ? usernameField : []);
			
		var rating = $.validator.passwordRating(password, username.val());
		// update message for this field
		
		var meter = \$(".password-meter", element.form);
		
		meter.find(".password-meter-bar").removeClass().addClass("password-meter-bar").addClass("password-meter-" + rating.messageKey);
		meter.find(".password-meter-message")
		.removeClass()
		.addClass("password-meter-message")
		.addClass("password-meter-message-" + rating.messageKey)
		.text($.validator.passwordRating.messages[rating.messageKey]);
		// display process bar instead of error message
		
		return rating.rate > 0;
	}, "&nbsp;");
	// manually add class rule, to make username param optional
	$.validator.classRuleSettings.password = { password: true };
	
})(jQuery);
</script>



<script id="demo" type="text/javascript">
\$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = \$("#signupform").validate({
		rules: {
			username: {
				required: true,
				minlength: 1
			},
			password: {
				password: "#username"
			},
			password_confirm: {
				required: true,
				equalTo: "#password"
			}
		},
		messages: {
			username: {
EOF
echo '				required: "' $"Preencha esse campo" '",'
cat << EOF
				minlength: jQuery.format("Enter at least {0} characters")
			},
			password_confirm: {
EOF
echo '				required: "' $"Repita a senha"  '",'
cat << EOF
				minlength: jQuery.format("Enter at least {0} characters"),
EOF
echo '				equalTo: "' $"Repita a senha corretamente" '"'
cat << EOF
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	
	// propose username by combining first- and lastname
	\$("#username").focus(function() {
		var firstname = \$("#firstname").val();
		var lastname = \$("#lastname").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});
	
});
</script>

      		<form action=submit-password.sh.htm name="signupform" id="signupform" method="get">
	  		  <table>
	  		  <tr>
EOF
echo '	  			<td class="label"><label id="lusername" for="username">' $"Usuario" '</label></td>'
cat << EOF
	  			<td class="field"><input id="username" onkeyup="this.value=this.value.replace(/[' 'ãÂẽÊĩĨõÕũŨâÂêÊîÎôÔûÛçÇáÁàÀéèÉÈíìÍÌóòÓÒúùÚÙñÑ~^@&,;/?<>}{]/g,'')" name="username" readonly="readonly" type="text" 
EOF
cat << EOF
				value="$p_username" maxlength="50" /></td>
				<input type="hidden" name="username" value="$p_username" />

	  			<td class="status"></td>
	  		  </tr>
	  		  <tr>
EOF
echo '	  			<td class="label"><label id="lpassword" for="password">' $"Senha" '</label></td>'
cat << EOF
	  			<td class="field"><input id="password" name="password" type="password" maxlength="50" value="" /></td>
	  			<td class="field">
	  				<div class="password-meter">
	  					<div class="password-meter-message">&nbsp;</div>
	  					<div class="password-meter-bg">
		  					<div class="password-meter-bar"></div>
	  					</div>
	  				</div>
				</td>
	  		  </tr>
	  		  <tr>
EOF
echo '	  			<td class="label"><label id="lpassword_confirm" for="password_confirm">' $"Confirme a senha" '</label></td>'
cat << EOF
	  			<td class="field"><input id="password_confirm" name="password_confirm" type="password" maxlength="50" value="" /></td>
	  			<td class="status"></td>
	  		  </tr>
	  		  </table>



EOF


echo "</div><div id=\"rodape\"><button type=submit id=\"signupsubmit\" value= $apply  >   <img src=\"/usr/share/bigcontrolcenter/default/images/ok.png\" style=\"vertical-align:top\" />  $apply   </button>  <button type=button value= $close onClick= parent.location='/usr/share/bigbashview/exemplo2.sh.htm?close=True'>   <img src=\"/usr/share/bigcontrolcenter/default/images/cancel.png\" style=\"vertical-align:top\"  />  $close     </button></div>"

